package main.java.LeetCode.SolutionVersionTwo;

@SuppressWarnings("ALL")
public class Trie {
    private Trie[] children;
    private boolean isEnd;

    public Trie(){
        children = new Trie[26];
        isEnd = false;
    }

    public Trie insert(String word){
        Trie node = this;
        for(int i = 0;i<word.length();i++){
            int index = word.charAt(i) - 'a';
            if(node.children[index]==null)
            {
                node.children[index] = new Trie();
            }
            node = node.children[index];
        }
        node.isEnd = true;
        return this;
    }

    public boolean search(String word,boolean isSubString){
        Trie node = this;
        String exitWord = "";
        if(isSubString){
            for(int i = 0;i<word.length();i++){
                if(node.children[word.charAt(i)-'a']==null)
                    return false;
                node = node.children[word.charAt(i)-'a'];
            }
        }
        else
        {
            for(int i = 0;i<word.length();i++){
                if(node.children[word.charAt(i)-'a']==null)
                    return false;
                node = node.children[word.charAt(i)-'a'];
            }
            if(!node.isEnd)
                return false;

        }
        return true;
    }
}
